#include "areaslectionmenu.h"

#include <QtGui>
#include "ktv/ktv.h"
#include "ktv/model/resource/resource.h"

AreaSlectionMenu::AreaSlectionMenu(KTV *k, int w, int h)
    : GUI(k), ktv(k), width(w), height(h)
{
    setFlag(QGraphicsItem::ItemIsFocusable);

    resource = Resource::getInstance();
    backgroundPixmap = resource->area_background;

    inland_ht = resource->areaselectionmenu_inland_ht;
    inland_ea = resource->areaselectionmenu_inland_ea;
    ht_inland = resource->areaselectionmenu_ht_inland;
    ht_ea = resource->areaselectionmenu_ht_ea;
    ea_inland = resource->areaselectionmenu_ea_inland;
    ea_ht = resource->areaselectionmenu_ea_ht;

    currMenu = 0;
    currList = 0;
    index_for_animation = 0;
    animation = new QPropertyAnimation(this, "index");
    animation->setEasingCurve(QEasingCurve::InOutSine);
    connect(animation, SIGNAL(finished()), this, SLOT(animationFinish()));
}

void AreaSlectionMenu::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Q:
        ktv->showMainMenu();
    case Qt::Key_Right:
        nextMenu();
        currMenu++;
        currMenu %= 3;
        qDebug() << "curr menu: " << currMenu;
        break;
    case Qt::Key_Left:
        previousMenu();
        currMenu--;
        if (currMenu < 0) {
            currMenu += 3;
        }
        qDebug() << "curr menu: " << currMenu;
        break;
    case Qt::Key_Y:
        ktv->selectAreaSelectionMenu(currMenu);
        break;
    }
}

void AreaSlectionMenu::select()
{
    ktv->selectAreaSelectionMenu(currMenu);
}

void AreaSlectionMenu::nextMenu()
{
    if (!(animation->state() == QAbstractAnimation::Stopped)) {
        currMenu--;
        return;
    }

    qDebug() << "menu: " << currMenu << " turn next";

    switch (currMenu) {
    case 0:
        currList = inland_ht;
        animation->setDuration(250);
        startAnimation();
        break;
    case 1:
        currList = ht_ea;
        animation->setDuration(1000);
        startAnimation();
        break;
    case 2:
        currList = ea_inland;
        animation->setDuration(750);
        startAnimation();
        break;
    }
}

void AreaSlectionMenu::previousMenu()
{
    if (!(animation->state() == QAbstractAnimation::Stopped)) {
        currMenu++;
        return;
    }

    qDebug() << "menu: " << currMenu << " turn previous";

    switch (currMenu) {
    case 0:
        currList = inland_ea;
        animation->setDuration(750);
        startAnimation();
        break;
    case 1:
        currList = ht_inland;
        animation->setDuration(250);
        startAnimation();
        break;
    case 2:
        currList = ea_ht;
        animation->setDuration(1000);
        startAnimation();
        break;
    }
}

void AreaSlectionMenu::startAnimation()
{
    qDebug() << "area menu is busy.";
    ktv->setGuiUsable(false);

    animation->setStartValue(0);
    animation->setEndValue(currList->size()-1);
    animation->start();
}

void AreaSlectionMenu::setIndexForAnimation(int index)
{
    index_for_animation = index;
    backgroundPixmap = currList->at(index);
    update();
}

QRectF AreaSlectionMenu::boundingRect() const
{
    return QRectF(-width/2.0, -height/2.0, width, height);
}

void AreaSlectionMenu::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(painter);
    Q_UNUSED(option);
    painter->drawPixmap(-width/2.0, -height/2.0, width, height, *backgroundPixmap);
}
